package com.mnt.washio.utils.network.ws;

import java.io.IOException;
import java.util.LinkedHashMap;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;

import com.mnt.washio.objects.Card;
import com.mnt.washio.utils.Statics;
import com.mnt.washio.utils.network.NetworkUtils;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;

@SuppressLint("SimpleDateFormat")
public class DeleteCardWSControl extends BaseWSControl {
	public String stripeId;
	public Card card;

	public DeleteCardWSControl(Context context,
			WebServiceCommunicatorListener listener, String stripeId,
			Card card) {
		super(context, listener, WebServiceFlag.REMOVE_CARD);
		this.stripeId = stripeId;
		this.card = card;
	}

	@Override
	protected Object doInBackground(Void... params) {
		// Get local database
		String url = Statics.ADMIN_WS_URL_NEW + "payment/creditCard?stripeId="+stripeId+"&cardId="+card.getId();
		LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>();
		LinkedHashMap<String, String> headers = new LinkedHashMap<String, String>();

		try {
			String respone = NetworkUtils.doDeleteRequest(url, parameters,
					headers);
			JSONObject jsonObject = new JSONObject(respone);
			if (jsonObject.has("error")) {
				mError = jsonObject.getString("error");
				return null;
			} else {
				return true;
			}
		} catch (ClientProtocolException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		} catch (IOException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		} catch (JSONException e) {
			mError = e.getMessage();
			e.printStackTrace();
		}
		return null;
	}

}
